Imports System.Configuration
Imports System.Data.Common
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Collections.Generic
Imports Microsoft.VisualBasic
Imports System.Collections.Specialized
Imports System
Imports System.Diagnostics
Imports System.Web
Imports System.Globalization
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Web.Hosting

Public Class FileImageProvider
    Inherits ImageProvider

    Private _PhotoFolder As String

    Public Overrides ReadOnly Property Name() As String
        Get
            Return "KDESolutions.Providers.Images.DBImageProvider"
        End Get
    End Property

    Public Overrides ReadOnly Property Description() As String
        Get
            Return "Provider for storing images"
        End Get
    End Property

    Public Overrides Sub Initialize(ByVal name As String, ByVal config As NameValueCollection)
        If (config Is Nothing) Then
            Throw New ArgumentNullException("config")
        End If
        '***************************************************************************
        '***************************************************************************
        '***************************************************************************
        'Begin of product registration
        Dim productKey As String = String.Empty
        Try
            If (config("ProductKey") Is Nothing) Then
                Dim newEx As New Exception("You must supply a product key!")
                Throw newEx
            ElseIf (config("ActivationDate") Is Nothing) Then
                Dim newEx As New Exception("You must supply an order date.")
                Throw newEx
            End If

            Dim tempProductKey As String = String.Empty
            Dim tempProductKey1 As String = String.Empty
            Dim productName As String = Me.GetType.Assembly.GetName.ToString.Split(",")(0)
            Dim productVersion As String = Me.GetType.Assembly.GetName.Version.ToString
            Dim orderDate As String = CType(config("ActivationDate"), Date).ToShortDateString



            'Encrypt the password
            Dim md5Hasher As New MD5CryptoServiceProvider()

            Dim hashedBytes As Byte()
            Dim encoder As New UTF8Encoding()
            tempProductKey = productName & productVersion & orderDate

            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(tempProductKey))
            For Each i As String In hashedBytes
                tempProductKey1 = tempProductKey1 & i & "-"
            Next
            tempProductKey1 = tempProductKey1.Substring(0, (tempProductKey1.Length - 1))

            productKey = tempProductKey1.Split("-")(0) & tempProductKey1.Split("-")(3) & "-" & _
            tempProductKey1.Split("-")(5) & tempProductKey1.Split("-")(2) & "-" & tempProductKey1.Split("-")(7) & tempProductKey1.Split("-")(9) & _
            "-" & tempProductKey1.Split("-")(1) & tempProductKey1.Split("-")(4)

        Catch
            Dim newEx As New Exception("Error validating your product key.")
            Throw newEx
        End Try

        If (config("ProductKey") = productKey) = False Then
            Throw New ArgumentException("Invalid Product Key!")
        End If
        'End of product registration
        '************************************************************************
        '***************************************************************************
        '***************************************************************************

        If (String.IsNullOrEmpty(config("PhotoFolder")) = False) Then
            _PhotoFolder = config("PhotoFolder")
        Else
            _PhotoFolder = "~/Photos"
        End If

        If ((name Is Nothing) OrElse (name.Length = 0)) Then
            name = "ImageProvider"
        End If
        If String.IsNullOrEmpty(config("description")) Then
            config.Remove("description")
            config.Add("description", "KDESolutions Image provider")
        End If
    End Sub

    Public Overrides Sub AddAlbum(ByVal Caption As String, ByVal IsPublic As Boolean, ByVal Username As String)

    End Sub

    Public Overrides Sub AddPhoto(ByVal AlbumID As String, ByVal Caption As String, ByVal BytesOriginal() As Byte, ByVal Username As String)

    End Sub

    Public Overrides Sub EditAlbum(ByVal Caption As String, ByVal IsPublic As Boolean, ByVal AlbumID As String, ByVal Username As String)

    End Sub

    Public Overrides Sub EditPhoto(ByVal Caption As String, ByVal PhotoID As String, ByVal Username As String)

    End Sub

    Public Overrides Function GetAlbums(ByVal PublicOnly As Boolean) As System.Collections.Generic.List(Of DataTypes.Album)

    End Function

    Public Overrides Function GetFirstPhoto(ByVal albumid As String, ByVal size As Enums.ImageEnumerations.PhotoSize, ByVal PublicOnly As Boolean) As System.IO.Stream

    End Function

    Public Overloads Overrides Function GetPhoto(ByVal size As Enums.ImageEnumerations.PhotoSize) As System.IO.Stream
        Dim path As String = HttpContext.Current.Server.MapPath("~/Images/")
        If (size = Images.Enums.ImageEnumerations.PhotoSize.Small) Then
            path = (path + "placeholder-100.jpg")
        Else
            If (size = Images.Enums.ImageEnumerations.PhotoSize.Medium) Then
                path = (path + "placeholder-200.jpg")
            Else
                If (size = Images.Enums.ImageEnumerations.PhotoSize.Large) Then
                    path = (path + "placeholder-600.jpg")
                Else
                    path = (path + "placeholder-600.jpg")
                End If
            End If
        End If
        Return New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
    End Function

    Public Overloads Overrides Function GetPhoto(ByVal photoid As String, ByVal size As Enums.ImageEnumerations.PhotoSize) As System.IO.Stream
        Try
            Select Case size
                Case 1
                    Return New MemoryStream(CType(System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(_PhotoFolder & photoid & "_Thumb_" & ".jpg")), Byte()))
                Case 2
                    Return New MemoryStream(CType(System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(_PhotoFolder & photoid & "_Poster_" & ".jpg")), Byte()))
                Case 3
                    Return New MemoryStream(CType(System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(_PhotoFolder & photoid & "_Full_" & ".jpg")), Byte()))
                Case 4
                    Return New MemoryStream(CType(System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(_PhotoFolder & photoid & "_Original_" & ".jpg")), Byte()))
                Case Else
                    Return New MemoryStream(CType(System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(_PhotoFolder & photoid & "_Poster_" & ".jpg")), Byte()))
            End Select
        Catch
            Return Nothing
        End Try
    End Function

    Public Overloads Overrides Function GetPhotos(ByVal PublicOnly As Boolean) As System.Collections.Generic.List(Of DataTypes.Photo)

    End Function

    Public Overloads Overrides Function GetPhotos(ByVal AlbumID As String, ByVal PublicOnly As Boolean) As System.Collections.Generic.List(Of DataTypes.Photo)

    End Function

    Public Overrides Function GetRandomPhoto() As System.Collections.Generic.List(Of DataTypes.Photo)

    End Function

    Public Overrides Sub RemoveAlbum(ByVal AlbumID As String, ByVal Username As String)

    End Sub

    Public Overrides Sub RemovePhoto(ByVal PhotoID As String, ByVal Username As String)

    End Sub
End Class
